package com.bulletelc.apollopre.module.main.center.orders.orderComment;

import android.content.Context;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageButton;
import android.widget.ImageView;

import com.bulletelc.apollopre.R;
import com.bulletelc.apollopre.constants.IntentCode;
import com.bulletelc.apollopre.manager.imageload.ImageLoaderManager;

import java.util.ArrayList;
import java.util.HashMap;

import me.iwf.photopicker.PhotoPicker;

/**
 * Created by CX on 2018/3/5.
 */

public class FinishCommentGridViewPicAdapter extends BaseAdapter implements View.OnClickListener{
    private ArrayList<HashMap<String, String>> imgPaths;
    private Context context;
    private LayoutInflater inflater;
    private onPicAddListener onPicAddListener;
    private onPicCancelListener onPicCancelListener;
    private int itemPos; // list位置

    public void setPicAddListener(onPicAddListener onPicAddListener) {
        this.onPicAddListener = onPicAddListener;
    }

    public void setPicCancelListener(onPicCancelListener onPicCancelListener) {
        this.onPicCancelListener = onPicCancelListener;
    }

    public FinishCommentGridViewPicAdapter(Context context, ArrayList<HashMap<String, String>> imgPaths, int itemPos) {
        super();
        this.context = context;
        this.imgPaths = imgPaths;
        this.itemPos = itemPos;
        this.inflater = LayoutInflater.from(context);
    }

    @Override
    public int getCount() {
        if (imgPaths == null) {
            return 0;
        } else {
            return imgPaths.size();
        }
    }

    @Override
    public Object getItem(int i) {
        return imgPaths.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {
        ViewHolder holder;
        HashMap<String, String> hashMap = imgPaths.get(i);
        if (view == null) {
            view = inflater.inflate(R.layout.comment_add_pic_item,null,false);
            holder = new ViewHolder(view);
            view.setTag(holder);
        } else {
            holder = (ViewHolder) view.getTag();
        }

        if (hashMap.get("content").equals("")) {
            holder.ivAddPic.setImageResource(R.drawable.order_common_pic_add);
            holder.ivAddPic.setTag(R.id.tag_order_comment_btn_picture, i);
            holder.ivAddPic.setOnClickListener(this);
            holder.iBtnCancel.setVisibility(View.GONE);
        } else {
            ImageLoaderManager.getInstance().loadImage(
                    ImageLoaderManager.getDefaultOptions(holder.ivAddPic, hashMap.get("content")));
            holder.ivAddPic.setTag(R.id.tag_order_comment_btn_picture, i);
            holder.ivAddPic.setOnClickListener(this);
            holder.iBtnCancel.setVisibility(View.VISIBLE);
            holder.iBtnCancel.setTag(R.id.tag_comment_gv_pic_cancel, i);
            holder.iBtnCancel.setOnClickListener(this);
        }

        return view;
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.ivAddPic:
                int imgPos = (int) view.getTag(R.id.tag_order_comment_btn_picture);
                FinishCommentActivity.imgPosition = imgPos;
                FinishCommentActivity.itemPosition = itemPos;
                if (imgPaths.get(imgPos).get("content").equals("")) {
                    PhotoPicker.builder()
                            .setPhotoCount(7-imgPaths.size())
                            .setShowCamera(true)
//                        .setSelected(imgPaths) //已选图片
                            .start((AppCompatActivity) context);
                } else {
                    Intent intent = new Intent(context, ImagePagerActivity.class);
                    intent.putExtra(IntentCode.EXTRA_KEY_IMAGE_CURRENT_POSITION, imgPos);
                    ArrayList<String> list = new ArrayList<>();
                    for (int i = 0; i < imgPaths.size(); i++) {
                        String path = imgPaths.get(i).get("content");
                        if (!path.equals("")) {
                            list.add(path);
                        }
                    }
                    intent.putStringArrayListExtra(IntentCode.EXTRA_KEY_IMAGES_PATH, (ArrayList<String>) list);
                    context.startActivity(intent);
                }
                break;
            case R.id.btnCancel:
                int cancelPos = (int) view.getTag(R.id.tag_comment_gv_pic_cancel);
                imgPaths.remove(cancelPos);
                if (imgPaths.size() < 6) {
                    for (int o = 0; o < imgPaths.size(); o++) {
                        if (imgPaths.get(o).get("content").equals("")) {
                            imgPaths.remove(o);
                        }
                    }
                    HashMap<String, String> hashMap = new HashMap<>();
                    hashMap.put("content", "");
                    imgPaths.add(hashMap);
                }
                notifyDataSetChanged();
                break;
        }
    }

    class ViewHolder {
        ImageView ivAddPic;
        ImageButton iBtnCancel;
        public ViewHolder(View v) {
            ivAddPic = v.findViewById(R.id.ivAddPic);
            iBtnCancel = v.findViewById(R.id.btnCancel);
        }
    }

    interface onPicAddListener {
        void click(int itemPos, int position);
    }

    interface onPicCancelListener {
        void cancelClick(int position);
    }
}
